package com.ruffian.widget.slice;

import com.ruffian.library.RTextView;
import com.ruffian.widget.ResourceTable;
import com.ruffian.widget.adapter.PageAdapter;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.global.resource.Resource;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * demo主页面
 * 类描述： MainAbilitySlice
 *
 * @author Administrator
 * @date 2021/5/26
 */
public class MainAbilitySlice extends AbilitySlice {
    private final static int PRESS_FALSE = 2048;
    private static final String LOG_FORMAT = "%{public}s: %{public}s";
    private List<Component> components = new ArrayList<>();
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "MainAbilitySlice");
    private PageAdapter pageAdapter;
    private RTextView text1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        addPageData();
        setPage();
    }

    private void addPageData() {
        Component fractionCorner = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_fraction_corner,
                        null,
                        false);
        components.add(fractionCorner);
        Component fractionState = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_fraction_state,
                        null,
                        false);
        text1 = (RTextView) fractionState.findComponentById(ResourceTable.Id_text7);
        setBackground(text1, ResourceTable.Media_icon_bg_normal);
        text1.setComponentStateChangedListener(new Component.ComponentStateChangedListener() {
            @Override
            public void onComponentStateChanged(Component component, int i) {
                if (ComponentState.isStateMatched(i, ComponentState.COMPONENT_STATE_PRESSED)) {
                    setBackground(text1, ResourceTable.Media_icon_bg_pressed);
                }
                if (i == PRESS_FALSE) {
                    setBackground(text1, ResourceTable.Media_icon_bg_normal);
                }
            }
        });
        components.add(fractionState);
        Component fractionVerson = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layout_fraction_version,
                        null,
                        false);
        components.add(fractionVerson);
    }

    private void setBackground(RTextView textView, int resId) {
        try {
            Resource resource = getAbility().getResourceManager().getResource(resId);
            Element element = new PixelMapElement(resource);
            textView.setBackground(element);
        } catch (Exception e) {
            HiLog.debug(LABEL_LOG, LOG_FORMAT, e.getMessage());
        }
    }

    private void setPage() {
        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageSlider.setOrientation(Component.HORIZONTAL);
        pageSlider.setPageCacheSize(1);
        pageSlider.setSlidingPossible(true);
        pageAdapter = new PageAdapter(components);
        pageSlider.setProvider(pageAdapter);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
